package ar.edu.sovietrussia.customcomponents.bookviews;

import java.util.List;

import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;

/**
 */
public class ListMiniBooksPanel extends Panel {

    private static final long serialVersionUID = 1L;

    public ListMiniBooksPanel(final String id, final ListBookModel listModel) {
        super(id, listModel);

        this.addComponents();
    }

    public ListMiniBooksPanel(final String id) {
        super(id, new ListBookModel());
        this.addComponents();
    }

    private void addComponents() {
        List<BookModel> list = this.getModel();
        ListView<BookModel> listview = new ListView<BookModel>("bookList", list) {
            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(final ListItem<BookModel> item) {
                item.add(new MiniBookViewPanel("miniBook", item.getModel().getObject()) {
                    private static final long serialVersionUID = 1L;

                    @Override
                    public void onTitleLinkClick(final BookModel model) {
                        ListMiniBooksPanel.this.onSelectedBook(model);
                    }
                });
            }
        };

        this.add(listview);
    }

    @SuppressWarnings("unchecked")
    private List<BookModel> getModel() {
        return (List<BookModel>) this.getDefaultModelObject();
    }

    public void onSelectedBook(final BookModel model) {
        // redefinir
    }

}
